package com.xiaolu.qqclient.view;

import com.xiaolu.qqclient.service.UserClientService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

/**
 * @Author：xiexu
 * @Date：2023/12/5 13:28
 */
public class Login extends JFrame {

    private UserClientService userClientService = new UserClientService();//对象是用于登录服务/注册用户
    private String img_path="com/xiaolu/qqclient/img/message.png";

    JLabel topNameLabel = new JLabel("网络即时通信系统", JLabel.CENTER);  // 设置第二个参数，使之居中显示

    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);  // centerPanel使用弹簧布局
    JLabel userNameLabel = new JLabel("用户名: ");
    JTextField userNameTextField = new JTextField();
    JLabel passwordLabel = new JLabel("密码: ");

    JLabel pfLabel = new JLabel("确认密码: ");
    JPasswordField passwordTextField = new JPasswordField(18);
    //放置确认密码
    JPasswordField pf2 = new JPasswordField(20);
    JButton loginButton = new JButton("登录");
    JButton resetButton = new JButton("注册");

    public Login() throws AWTException {  // 构造函数
        super("网络即时通信系统"); // 设置标题
        Container contentPane = getContentPane(); // 获取内容面板

        // 自定义图标：
        URL url = Login.class.getClassLoader().getResource(img_path);
        assert url != null;
        Image image = new ImageIcon(url).getImage();
        setIconImage(image);

        // 设置字体：
        Font font = new Font("宋体", Font.PLAIN, 40);
        topNameLabel.setFont(font);
        topNameLabel.setPreferredSize(new Dimension(0, 80));

        Font font1 = new Font("楷体", Font.PLAIN, 20);
        userNameLabel.setFont(font1);
        userNameTextField.setPreferredSize(new Dimension(200, 30));  // 设置文本框大小
        passwordLabel.setFont(font1);
        passwordTextField.setPreferredSize(new Dimension(200, 30));
        loginButton.setFont(font1);
        resetButton.setFont(font1);
        pf2.setFont(font1);
        pfLabel.setFont(font1);

        // 将所有组件都加入到中间面板中：  centerPanel
        centerPanel.add(userNameLabel);
        centerPanel.add(userNameTextField);
        centerPanel.add(passwordLabel);
        centerPanel.add(passwordTextField);
        centerPanel.add(loginButton);
        centerPanel.add(resetButton);
        centerPanel.add(pfLabel);
        centerPanel.add(pf2);
        pfLabel.setVisible(false);
        pf2.setVisible(false);
        // 将标题加入到面板的北部：
        contentPane.add(topNameLabel, BorderLayout.NORTH);
        // 将中心面板加入到内容面板中：
        contentPane.add(centerPanel, BorderLayout.CENTER);


        // 设置中心面板中内容的弹簧布局
        // 1. 布局 userNameLabel  (是下方组件的参照物！)
        Spring userNameLabelWidth = Spring.width(userNameLabel);
        Spring userNameTextFieldWidth = Spring.width(userNameTextField);
        Spring springWidth = Spring.constant(20); // 左右间距
        Spring childWidth = Spring.sum(Spring.sum(userNameLabelWidth, userNameTextFieldWidth), springWidth);
        int offsetX = childWidth.getValue() / 2; // 得到水平偏移量

        springLayout.putConstraint(SpringLayout.WEST, userNameLabel, -offsetX, SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameLabel, 20, SpringLayout.NORTH, centerPanel);

        // 2.userNameTextField
        springLayout.putConstraint(SpringLayout.WEST, userNameTextField, 5, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameTextField, 0, SpringLayout.NORTH, userNameLabel);

        // 3.passwordLabel
        springLayout.putConstraint(SpringLayout.EAST, passwordLabel, 0, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, passwordLabel, 20, SpringLayout.SOUTH, userNameLabel);

        // 4.passWordTextField
        springLayout.putConstraint(SpringLayout.WEST, passwordTextField, 5, SpringLayout.EAST, passwordLabel);
        springLayout.putConstraint(SpringLayout.NORTH, passwordTextField, 0, SpringLayout.NORTH, passwordLabel);

        // 5.pfLabel
        springLayout.putConstraint(SpringLayout.EAST, pfLabel, 0, SpringLayout.EAST, passwordLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pfLabel, 20, SpringLayout.SOUTH, passwordLabel);

        // 6.pf2
        springLayout.putConstraint(SpringLayout.WEST, pf2, 5, SpringLayout.EAST, pfLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pf2, 0, SpringLayout.NORTH, pfLabel);

        // 7. loginButton
        springLayout.putConstraint(SpringLayout.WEST, loginButton, 40, SpringLayout.WEST, pfLabel);
        springLayout.putConstraint(SpringLayout.NORTH, loginButton, 40, SpringLayout.SOUTH, pfLabel);

        // 8.resetButton
        springLayout.putConstraint(SpringLayout.WEST, resetButton, 60, SpringLayout.EAST, loginButton);
        springLayout.putConstraint(SpringLayout.NORTH, resetButton, 0, SpringLayout.NORTH, loginButton);
        setSize(600, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);



        // 托盘：
        // 系统托盘
        SystemTray systemTray;
        // 自定义一个托盘
        TrayIcon trayIcon;

        // 首先判断系统是否支持
        if (SystemTray.isSupported()) {
            systemTray = SystemTray.getSystemTray();
            URL imgUrl = Login.class.getClassLoader().getResource(img_path);
            assert imgUrl != null;
            trayIcon = new TrayIcon(new ImageIcon(imgUrl).getImage());
            systemTray.add(trayIcon); // 将自定义的托盘加入到系统托盘中
            trayIcon.setImageAutoSize(true); // 设置托盘图片大小自动缩放， 一定要设置，不然图标可能显示不出来

            // 最小化的时候销毁窗口
            this.addWindowListener(new WindowAdapter() {
                @Override
                public void windowIconified(WindowEvent e) {
                    Login.this.dispose();
                }
            });

            // 鼠标点击一次，让窗口正常显示
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int clickCount = e.getClickCount();
                    if (clickCount == 1) {
                        Login.this.setExtendedState(JFrame.NORMAL);
                    }
                    Login.this.setVisible(true);
                }
            });
            //显示确认密码的窗口
            resetButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    pfLabel.setVisible(true);
                    pf2.setVisible(true);
                }
            });
            //关闭确认密码的窗口
            loginButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //读取用户名和密码
                    String userID = userNameTextField.getText();
                    String pwd = String.valueOf(passwordTextField.getPassword());
                    System.out.println(userID);
                    System.out.println(pwd);

                    if (userClientService.checkUser(userID, pwd)){
                        System.out.println("登录成功");

                        //校验用户名、密码
                        pfLabel.setVisible(false);
                        pf2.setVisible(false);
                        dispose();
                        //切换到新界面
                        ChatRoomGUI gui = ChatRoomGUI.getInstance(userID,userClientService);
                        gui.setVisible(true);
                    }else {
                        System.out.println("登录失败");
                    }


                }

            });
        }
    }
    public static void main(String[] args) throws AWTException {
        new Login();
    }
}